package ListManager;

import java.util.ArrayList;

import model.File;
import ui.FileListActivity;
import utils.Tools;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ui.R;

public class FileListAdapter extends BaseAdapter implements OnClickListener {
		
	 private Activity activity;
	 private ArrayList<File> data;
	 private static LayoutInflater inflater=null;
	 public Resources res;
	 private File tempValues=null;
	 private ArrayList<Integer> mSelectedItems = new ArrayList<Integer>();
	 
	 public FileListAdapter(Activity a, ArrayList<File> d,Resources resLocal) {   
	     activity = a;
	     data=d;
	     res = resLocal;
	     inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	 }
	 
	 public int getCount() {
		 if(data.size()<=0)
			 return 1;
		 
	         return data.size();
	 }
	 
     public Object getItem(int position) {
         return position;
     }
  
     public long getItemId(int position) {
         return position;
     }
     
     public static class ViewHolder{    
         public TextView name;
         public TextView last;
         public TextView size;
         public TextView extension;
     }
	  
	 public View getView(int position, View convertView, ViewGroup parent) {	      
	     View vi = convertView;
	     ViewHolder holder;
	     
	     if(convertView==null){
	        vi = inflater.inflate(R.layout.filetabitem, null);
	         
	        TextView nameTV = (TextView) vi.findViewById(R.id.fileNameTextView);
	        TextView extTV = (TextView) vi.findViewById(R.id.extensionTextView);
	        TextView sizeTV = (TextView) vi.findViewById(R.id.sizeTextView);
	        TextView lastTV = (TextView) vi.findViewById(R.id.lastModTextView);
	        
	        Typeface font = Typeface.createFromAsset(activity.getAssets(), "font/capt.ttf");
	        nameTV.setTypeface(font);
	        extTV.setTypeface(font);
	        sizeTV.setTypeface(font);
	        lastTV.setTypeface(font);
	        
            holder = new ViewHolder();
	        holder.name = nameTV;
	        holder.extension = extTV;
	        holder.size = sizeTV;
	        holder.last = lastTV;
	        vi.setTag( holder );
	     }
	     else {
	         holder=(ViewHolder)vi.getTag();
	     }
	      
	     if(data.size()>0)
	     {
	         tempValues = ( File ) data.get( position );
	         
	         if(mSelectedItems.contains(position)){
	        	 vi.setBackgroundColor(res.getColor(R.color.bluePale));
	         }
	         else{
	        	 vi.setBackgroundColor(res.getColor(R.color.blue));
	         }
	         
    		//instanciation object
	         holder.name.setText( tempValues.getName());
	         holder.extension.setText(tempValues.getExtension());
             holder.size.setText(tempValues.getSize()+" kB");
             holder.last.setText( Tools.convertDateToString(tempValues.getLastModified()));
  
             vi.setOnClickListener(new OnItemClickListener(position));
	     }
	     else{
	    	 holder.name.setText("");
	         holder.extension.setText("");
             holder.size.setText("");
             holder.last.setText("");
             vi.setBackgroundColor(res.getColor(R.color.black));
	     }
	     return vi;
	 }
	          	  
     private class OnItemClickListener  implements OnClickListener{           
    	 private int mPosition;
              
         OnItemClickListener(int position){	 
             mPosition = position;
         }       
          
         @Override
         public void onClick(View arg0) {
        	 FileListActivity sct = (FileListActivity)activity;
        	 if(mSelectedItems.contains(mPosition)){
        		 int index = mSelectedItems.indexOf(mPosition);
        		 mSelectedItems.remove(index);
        		 sct.onFileItemClick(mPosition);
        	 }
        	 else{
        		 mSelectedItems.add(mPosition);
        		 sct.onFileItemClick(mPosition);
        	 }
        	 notifyDataSetChanged();
        }              
     }

	@Override
	public void onClick(View v) {
		
	}   
	
	public ArrayList<Integer> getSelectedItems(){
		return mSelectedItems;
	}
	
	public void addSelectedItem(int pos){
		mSelectedItems.add(pos);
	}	
	
	
}